package com.atguigu.flink.chapter07.state;

import org.apache.flink.api.common.state.BroadcastState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ReadOnlyBroadcastState;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.BroadcastConnectedStream;
import org.apache.flink.streaming.api.datastream.BroadcastStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @Author lzc
 * @Date 2022/7/8 14:11
 */
public class Flink02_BoradCast {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
        env.enableCheckpointing(2000);
        
        MapStateDescriptor<String, String> desc = new MapStateDescriptor<>("confState", String.class, String.class);
        
        // 1. 需要两个流: 数据流  配置流
        DataStreamSource<String> dataStream = env.socketTextStream("hadoop162", 8888);
        DataStreamSource<String> confStream = env.socketTextStream("hadoop162", 9999);
        
        // 2. 把配置流做成广播流
        BroadcastStream<String> bcStream = confStream.broadcast(desc);
        
        // 3. 让数据流去 connect 广播流
        BroadcastConnectedStream<String, String> connectedStream = dataStream.connect(bcStream);
        
        connectedStream
            .process(new BroadcastProcessFunction<String, String, String>() {
                
                @Override
                public void processElement(String value, ReadOnlyContext ctx, Collector<String> out) throws Exception {
                    // 5. 处理数据流中的数据: 从广播状态中读取配置信息
                    System.out.println("数据流: " + value);
    
                    ReadOnlyBroadcastState<String, String> state = ctx.getBroadcastState(desc);
                    String aSwitch = state.get("switch");
    
                    if ("2".equals(aSwitch)) {
                        out.collect(Math.pow(Integer.parseInt(value), 2) + "");
                    }else if("3".equals(aSwitch)){
                        out.collect(Math.pow(Integer.parseInt(value), 3) + "");
                    }else if("4".equals(aSwitch)){
                        out.collect(Math.pow(Integer.parseInt(value), 4) + "");
                    }else{
                        out.collect(Math.pow(Integer.parseInt(value), 5) + "");
                    }
    
    
                }
                
                @Override
                public void processBroadcastElement(String value, Context ctx, Collector<String> out) throws Exception {
                    // 4. 处理广播流的数据, 把广播流中的数据存入到广播状态
                    System.out.println("广播流: " + value);
                    BroadcastState<String, String> state = ctx.getBroadcastState(desc);
                    state.put("switch", value);
                    
                }
            })
            .print();
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
}
/*
广播
 */
